W6. Least Squares Geometry
1. Theory
1.1 The Least Squares Problem
In many real-world applications, we encounter systems of linear equations
Rather than declaring defeat, we ask a better question: What is the best approximate solution? This is the least squares problem.
Definition (Least-Squares Solution): Given
where
1.1.1 Motivating Example: Line Fitting
Suppose we want to find the best line
No single line passes through all three points (try it — you’ll get a contradiction). The least-squares approach finds the line that minimizes the total squared vertical distance from the data points to the line.
1.1.2 Applications
Least squares appears everywhere:
- Statistics: Linear regression (fitting models to data)
- Machine learning: Training linear models
- Physics and engineering: Fitting experimental data to theoretical models
- Signal processing: Filter design and system identification
- Economics: Estimating demand curves and cost functions
- Computer vision: Camera calibration and pose estimation
1.2 Geometric Interpretation: Projection onto the Column Space
The key geometric insight is this:
That closest point is the orthogonal projection of
The error vector
1.3 The Normal Equations
The orthogonality condition
Expanding this gives the normal equations:
This is always a consistent system. If
1.3.1 Derivation via Calculus (Alternative Approach)
We can also derive the normal equations by minimizing
Taking the gradient and setting it to zero:
This confirms the normal equations from a calculus perspective.
1.3.2 Properties of
The matrix
- Square:
- Symmetric:
- Positive semi-definite:
- Invertible if and only if
has full column rank (columns linearly independent)
1.3.3 The Pseudoinverse
When
so that
The pseudoinverse is central to numerical linear algebra and appears throughout machine learning and signal processing as the canonical way to “invert” a non-square or rank-deficient mapping.
1.4 Projection Matrices
1.4.1 Projection onto a Subspace
For a subspace
When
This means we can define a projection matrix
This matrix
Special case: projection onto a line. If
1.4.2 Properties of Projection Matrices
A matrix
- Idempotent:
(projecting twice gives the same result as projecting once) - Symmetric:
These can be verified for
✓ ✓ (using symmetry of )
Additional properties:
- For any
, is also a projection matrix, projecting onto (the left null space, orthogonal complement of ) is not invertible. The projection matrix projects every vector in onto the column space , which has dimension (assuming is not square of full rank). Any vector in the orthogonal complement satisfies , so has a nontrivial null space and is therefore singular (not invertible). Geometrically, collapses the entire orthogonal complement to zero, making it impossible to recover the original vector from alone.
1.4.3 Proof that for Projection onto a Line
For
The scalar
1.5 Data Fitting with Least Squares
1.5.1 Fitting a Line
Given
The least-squares solution via normal equations
Key fact: The best-fit line always passes through the point of averages
Proof. The first row of the normal equations
Dividing both sides by
This is precisely the condition that the line
1.5.2 Fitting a Plane and Higher Dimensions
The same idea extends to fitting higher-dimensional surfaces. To fit a plane
The normal equations
1.6 QR Decomposition for Least Squares
1.6.1 Why QR is Better
Solving the normal equations
The QR decomposition avoids forming
1.6.2 Derivation
Factor
If
1.6.3 Solving via Back Substitution
Since
- Compute the QR decomposition:
(using Gram-Schmidt or Householder reflections) - Compute
- Solve
by back substitution
Advantages over normal equations:
- No need to form
(avoids squaring the condition number) - Back substitution is efficient (
instead of for inversion) - Numerically stable and preferred in practice
1.6.4 SVD as an Alternative for Large or Rank-Deficient Problems
For large-scale or rank-deficient problems, the Singular Value Decomposition (SVD) provides the most general and numerically robust approach to least squares. Any
where
where
Why SVD over QR or normal equations?
- It handles rank-deficient
gracefully (zero singular values are simply ignored). - It avoids the numerical hazards of forming
. - It reveals the intrinsic geometry (rank, condition number, null space) of the problem.
- It is the method of choice in machine learning (e.g., principal component analysis) and large-scale scientific computing.
The condition number of
2. Definitions
- Overdetermined System: A system
with more equations than unknowns ( ); usually has no exact solution. - Least-Squares Solution: The vector
that minimizes , i.e., the sum of squared residuals. - Residual (Error) Vector:
; the difference between the observed values and the model’s predictions. In a least-squares solution, . - Normal Equations: The system
, whose solution gives the least-squares solution. Derived from the orthogonality condition . - Projection onto a Subspace: For a subspace
, the projection of onto is the unique vector minimizing . The error is orthogonal to . - Projection Matrix: A matrix
such that is the orthogonal projection of onto a subspace. For projection onto : . Characterized by (idempotent) and (symmetric). - Idempotent Matrix: A matrix
satisfying . Applying the projection twice gives the same result as applying it once. - Pseudoinverse (Moore-Penrose left inverse): For a matrix
with full column rank, . It satisfies and gives the least-squares solution . - Point of Averages: The center point
through which every least-squares best-fit line passes, where and are the means of the independent and dependent variable values, respectively. - QR Decomposition: A factorization
where is with orthonormal columns ( ) and is upper triangular with positive diagonal entries. - Full Column Rank: A matrix
has full column rank if , i.e., its columns are linearly independent. This guarantees is invertible and the least-squares solution is unique.
3. Formulas
- Normal Equations:
- Least-Squares Solution (when
has full column rank): - Projection of
onto : - Projection Matrix onto
: - Projection onto a Line (spanned by vector
): , - Complementary Projection (onto left null space):
, where projects onto - Projection Matrix Properties:
(idempotent) and (symmetric) - QR Least-Squares System:
(solved by back substitution) - Computing
from and : - Residual Norm (minimum error):
- Normal Equations for Line Fitting (
): - Pseudoinverse:
(left inverse when has full column rank)
4. Practice
4.1. Is the Projection Matrix Invertible? (Lab 5, Task 1)
Is the projection matrix
Click to see the solution
Key Concept: The rank of an outer product
The matrix
Every column of
(for )- The columns are linearly dependent, making
Answer: No,
4.2. Projection onto a Line and its Perpendicular (Lab 5, Task 2)
Let
(a) Find the projection matrix
(b) Compute
Click to see the solution
Key Concept: Two complementary projection matrices onto perpendicular lines must sum to
(a) Finding
For
The line perpendicular to
(b) Sum and product:
Explanation: Since
Answer:
4.3. Least Squares to Fit a Plane (Lab 5, Task 3)
We want to fit a plane
| 3 | 1 | 1 |
| 5 | 2 | 1 |
| 6 | 0 | 3 |
| 0 | 0 | 0 |
(a) Find the (overdetermined) system
(b) Set up and solve the
Click to see the solution
Key Concept: Substitute each data point into the plane equation to get one row per data point, then apply the least-squares formula.
(a) Setting up
Substituting each
(b) Setting up the normal equations:
Compute
The normal equations are:
Solving (using the inverse of
Answer: The best-fit plane is
4.4. Projection of onto the Column Space of (Lab 5, Task 4)
Find the projection of
Split
Click to see the solution
Key Concept: The projection
Compute
and its inverse:Compute the projection matrix
:Compute
:Compute
:Identify the subspace for
: Since is orthogonal to , it must lie in (the left null space). Verify: ✓
Answer:
4.5. Least Squares Solution and Perpendicularity Verification (Lab 5, Task 5)
Solve
Verify that the error
Click to see the solution
Key Concept: The least-squares error is always perpendicular to
Set up and solve the normal equations
: , so:Compute the projection
:Compute the error
:Verify orthogonality (let
and ):
Answer:
4.6. Best-Fit Line and the Point of Averages (Lab 5, Task 6)
The data points are
(a) Find the best-fit line
(b) Explain algebraically why
Click to see the solution
Key Concept: The least-squares line always passes through the center of mass (point of averages) of the data. This follows directly from the first normal equation.
(a) Finding the best-fit line:
The design matrix and observation vector are:
Compute the normal equation components:
Solving the system
Check:
(b) Algebraic explanation:
The first normal equation comes from the first row of
Dividing both sides by
This proves that the least-squares line always passes through the point of averages
Answer: Best-fit line is
4.7. Least Squares via QR Decomposition (Lab 5, Task 7)
Find the least-squares solution of
with QR decomposition:
Click to see the solution
Key Concept: With QR decomposition
Compute
:Solve
by back substitution:The system is:
- From row 3:
- From row 2:
- From row 1:
- From row 3:
Answer:
4.8. Orthogonal Basis via Gram-Schmidt (Assignment 5, Task 1)
Given vectors
Click to see the solution
Key Concept: Gram-Schmidt successively removes the component of each new vector that lies along previously computed orthogonal vectors:
- Set
. - Compute the projection coefficient:
- Subtract the projection:
- Verify orthogonality:
Answer: An orthogonal basis for
4.9. Orthogonal Basis Construction (Assignment 5, Task 2)
Given
Click to see the solution
- Set
. - Compute the projection coefficient:
- Subtract the projection:
- Verify:
✓
Answer: An orthogonal basis for
4.10. Orthogonal Basis Construction in (Assignment 5, Task 3)
Given
Click to see the solution
Set
, with .Compute projection coefficient:
Subtract the projection:
From the assignment:
.Verify:
✓
Answer: An orthogonal basis for
4.11. Orthogonal Basis Construction with Parameters (Assignment 5, Task 4)
Given
Click to see the solution
- Set
. - Compute the projection coefficient:
- Subtract the projection (
): - Verify:
✓
Answer: An orthogonal basis is
4.12. Orthogonal Basis in (Assignment 5, Task 5)
Given
Click to see the solution
- Set
. - Compute projection coefficient:
- Subtract the projection:
- Verify:
✓
Answer: An orthogonal basis is
4.13. Orthogonal Basis in with Three Vectors (Assignment 5, Task 6)
Given
Click to see the solution
- Set
, with . - Compute projection coefficient:
- Subtract the projection:
- Verify:
✓
Answer: An orthogonal basis is
4.14. Compute from and (Assignment 5, Task 7)
Given that
Click to see the solution
Key Concept: In QR decomposition,
Computing entry by entry:
Answer:
4.15. Compute from and for a Second Matrix (Assignment 5, Task 8)
Given:
Compute
Click to see the solution
Answer:
4.16. Normal Equations and Least-Squares Solution (Assignment 5, Task 9)
For the system
(a) Write the normal equations
(b) Find the least-squares solution
Click to see the solution
Compute
and :
(a) The normal equations are:
(b) Compute
Answer:
4.17. Solve a Least-Squares Problem via the Normal Equations (Assignment 5, Task 10)
For
(a) Write the normal equations. (b) Find
Click to see the solution
- Compute:
(a) Normal equations:
(b)
Answer:
4.18. Derive the Normal Equations for a Data-Fitting Problem (Assignment 5, Task 11)
For
(a) Write the normal equations. (b) Find
Click to see the solution
(a) Normal equations:
(b)
Answer:
4.19. Solve a Second Least-Squares Problem via the Normal Equations (Assignment 5, Task 12)
For
(a) Write the normal equations. (b) Find
Click to see the solution
(a) Normal equations:
(b)
Answer:
4.20. Least-Squares Solution with a Free Variable (Assignment 5, Task 13)
Find all least-squares solutions of
Click to see the solution
Key Concept: When
From the reduced row echelon form,
- From row 2:
- From row 1:
All least-squares solutions have the form:
Answer:
4.21. Least-Squares Solution with a Free Variable for a Second System (Assignment 5, Task 14)
The normal equations reduce to:
Find all least-squares solutions.
Click to see the solution
From the RREF,
Answer:
4.22. Least Squares via QR Decomposition (Assignment 5, Task 15)
Given
Click to see the solution
Key Concept: With QR decomposition, solve
Answer:
4.23. Least Squares via QR Decomposition for a Second System (Assignment 5, Task 16)
Given
Click to see the solution
Solve
- From row 2:
- From row 1:
Answer:
4.24. Projection Matrix from a Lecture Example (Lecture 5, Example 1)
Find the projection matrix onto the column space of:
Click to see the solution
Key Concept: The projection matrix onto
Compute
:Compute
:Compute the projection matrix
:Verify:
and ✓ (the matrix is symmetric by inspection)
Answer:
4.25. Best-Fit Line Through Three Points (Lecture 5, Example 2)
Find the best-fit line
Click to see the solution
Key Concept: Set up the overdetermined system
Set up the system
:Compute
and :Solve the normal equations:
Using the
inverse formula with :
Answer: The best-fit line is
4.26. Best-Fit Line to Five Data Points (Lecture 5, Example 3)
Fit a line
Click to see the solution
Key Concept: Form the design matrix, compute the normal equations, and solve.
Set up
:Compute
and :(since
, )(since
)Solve the normal equations:
From the second equation:
.Substituting into the first:
.Then
.
Answer: The least-squares line is
4.27. No Matrix with Given Row Space and Null Space (Tutorial 5, Task 1)
Prove there is no matrix whose row space contains
Click to see the solution
Key Concept: The row space
Let
Since
Answer: No such matrix exists, because any vector in the row space must be orthogonal to any vector in the null space, but
4.28. Constructing Matrices with Prescribed Subspaces (Tutorial 5, Task 2)
For each case, construct a matrix with the required property or prove it is impossible:
(1) Column space contains
(2) Row space contains
(3)
(4) Every row is orthogonal to every column (
(5) The columns add to a column of
Click to see the solution
(1) Possible. We need
Require
So
(2) Impossible. The row space vectors
The second vector
(3) Impossible.
(4) Possible. If every row is orthogonal to every column, then
Row 1 =
(5) Impossible. If columns add to
4.29. Smallest Subspace of the Matrix Space (Tutorial 5, Task 3)
Describe the smallest subspace of the
(1)
(2)
(3)
(4)
Click to see the solution
Key Concept: The smallest subspace containing a set of vectors (or matrices) is the span of those vectors.
(1) All linear combinations
(2)
(3) All scalar multiples
(4) Combining all three with coefficients
4.30. Squaring the Projection Matrix (Tutorial 5, Task 4)
Let
Click to see the solution
Key Concept: The scalar
Since
4.31. Best-Fit Line to Four Data Points (Tutorial 5, Task 5)
Find the best straight-line fit (least squares)
Then find the projection of
Click to see the solution
Key Concept: The least-squares line minimizes total squared vertical errors. The projection formula then gives
Set up the system:
Compute
and :(Check:
; ; ; .)Solve the normal equations:
The best-fit line is
.Find the projection
:Using the projection formula
:
Answer: Best-fit line:
4.32. Projection Matrix onto a Subspace (Tutorial 5, Task 6)
Find the projection matrix
Click to see the solution
Key Concept: Form the matrix
Form
:Compute
:(
is diagonal because — the columns are already orthogonal!)Compute
:Compute
:Verify:
and ✓
Answer: